﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net.Appender;
using Telerik.Web.UI;
using Aspose.Excel;

public partial class BlastOffReport : BasePage
{
    #region "Page proprties"
    public override string Icon
    {
        get
        {
            return "images/report_icon.png";
        }
    }
    #endregion

    #region "Page events"
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FilldropLstSelection();
        }
    }
    #endregion
    
    protected void lbExport_Click(object sender, ImageClickEventArgs e)
    {
      DataLayer dal = new DataLayer();
      string message = string.Empty;

      DataTable dt = null;
      
      string name = tbName.Text;
      decimal? sectionId = null;
      if (dropLstSection.SelectedIndex > 1)
      {
        sectionId = Decimal.Parse(dropLstSection.SelectedItem.Value);
      }
      dt = dal.GetBlastOffReportData(PAConfiguration.ConnectionString, Teacher.TEACHER_ID, name, sectionId, out message);

      int startRow = 1;
      string[] listFields = new string[] { "lastfirst", "STUDENT_ID", "course_name", "start_day", 
        "FIRST_DATE_ACCESSED", "last_date_accessed", "days_since_last_access", 
        "last_date_submitted", "days_since_submitted", "total_time_in_min_prev_week", "total_time_in_minutes",
        "percent_complete", "GRADE_TO_DATE"};
      Excel ex = Utils.GridToExcel(dt, listFields, "Blast Off Report", false, ref startRow);
      Worksheet ws = ex.Worksheets[0];
      for (int i = 1; i < startRow - 1; ++i)
      {
        for (int j = 0; j < listFields.Length; ++j)
        {

          ws.Cells[i, j].Style.Borders[BorderType.LeftBorder].Color = Color.Black;
          ws.Cells[i, j].Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;

          ws.Cells[i, j].Style.Borders[BorderType.RightBorder].Color = Color.Black;
          ws.Cells[i, j].Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

          ws.Cells[i, j].Style.Borders[BorderType.TopBorder].Color = Color.Black;
          ws.Cells[i, j].Style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;

          ws.Cells[i, j].Style.Borders[BorderType.BottomBorder].Color = Color.Black;
          ws.Cells[i, j].Style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
        }

        ws.Cells[i, 12].Style.Pattern = BackgroundType.Solid;
        ws.Cells[i, 12].Style.ForegroundColor = Color.Gray;
      }

      
        
      ex.Save("Blast Off Report Download.xls", FileFormatType.Excel2003, SaveType.OpenInExcel, Page.Response);
      Page.Response.End();
    }

    protected void rgBlastOffReport_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        LoadData();
    }
  
    protected void btnGo_Click(object sender, EventArgs args)
    {
        LoadData();
        rgBlastOffReport.Rebind();
    }
    
    #region "Helper Functions"
    /// <summary>
        /// Fill dropLstSelection
        /// </summary>
        private void FilldropLstSelection()
        {
            DataLayer dal = new DataLayer();
            string message;

            dropLstSection.Items.Clear();

            dropLstSection.DataSource = dal.GetSections(PAConfiguration.ConnectionString, out message);
            dropLstSection.DataValueField = "section_id";
            dropLstSection.DataTextField = "section_title";
            dropLstSection.DataBind();

            dropLstSection.Items.Insert(0, new ListItem("Select a section", "0"));
        }

        /// <summary>
        /// Fill rgBlastOffReport Data
        /// </summary>
        private void LoadData()
        {
            DataLayer dal = new DataLayer();
            string message;

            DataTable dt = null;

            string name = tbName.Text;

            decimal? sectionId = null;
            if (dropLstSection.SelectedIndex > 1)
            {
                sectionId = Decimal.Parse(dropLstSection.SelectedItem.Value);
            }

            dt = dal.GetBlastOffReportData(PAConfiguration.ConnectionString, Teacher.TEACHER_ID, name, sectionId, out message);

            if (dt == null)
            {
                rgBlastOffReport.DataSource = null;
                return;
            }

            if (!string.IsNullOrEmpty(Sorting))
            {
                dt.DefaultView.Sort = Sorting;
            }

            rgBlastOffReport.DataSource = dt;
        }

        #endregion
}